package com.li.test.base.socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client01 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1",8000);
            OutputStream out = socket.getOutputStream();
            System.out.println("TCP连接成功 \n请输入：");
            while (true){
                byte[] car = new Scanner(System.in).nextLine().getBytes();
                out.write(car);
                System.out.println("TCP协议的socket发送成功");

                out.write("第二次发送".getBytes());
                InputStream in = socket.getInputStream();

                byte[] buffer = new byte[1024];
                int len =0 ;
                //读取客户端数据
                while ((len = in.read(buffer)) > 0){
                    System.out.println("读取服务端发来的数据："+new String(buffer,0,len));

                }
                //刷新缓冲区
                out.flush();
               // out.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
